﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Shared.Model;
using Client;

namespace ManageSystem.Manager
{
    public partial class FormMac : UserControl
    {
        private List<PRODUCT> _lockProduct = null;
        private List<Device_Data> _listDevice = new List<Device_Data>();
        private List<NODE> _listNode = null;

        public FormMac()
        {
            InitializeComponent();
        }

        private void buttonRefush_Click(object sender, EventArgs e)
        {
            this.listViewLockMac.Items.Clear();
            _listDevice.Clear();//清空LOCK

            PRODUCT proW = new PRODUCT();
            proW.SetWhere();

            proW.BREAK_STATE = true;

            var selectProduct = MethodWcf.SelectProductClient(proW);
            if (selectProduct.result)
            {
                _lockProduct = selectProduct.value;
                for(int i = 0; i < _lockProduct.Count; i++)
                {
                    SetOneLockProduct(_lockProduct[i].MAC);
                }
            }
        }

        private void SetOneLockProduct(string mac)
        {
            ListViewItem item = new ListViewItem();
            item.Text = mac;

            PRODUCT product = (from p in _lockProduct
                           where p.MAC == mac || p.YCD_MAC==mac
                           select p).FirstOrDefault();

            if (product == null) return;

            item.SubItems.Add(product.LINE_ID.ToString());

            var nodeName = (from n in _listNode
                            where n.ID == product.NODE_ID
                            select n.NAME).FirstOrDefault();

            item.SubItems.Add(nodeName);

            Device_Data device = (from d in _listDevice
                                  where d.MACAddr == product.MAC
                                  select d).FirstOrDefault();

            if (device == null)
            {
                Device_Data deviceW = new Device_Data();
                deviceW.SetWhere();
                deviceW.MACAddr = product.MAC;

                var selectDevice = MethodWcf.SelectDeviceDataClient(deviceW);
                if (selectDevice.result)
                {
                    _listDevice.Add(selectDevice.value[0]);
                    device = selectDevice.value[0];
                }
            }

            if (device!=null&&string.IsNullOrEmpty(device.LightModuleSN) == false && device.LightModuleSN.Length > 3)
                item.SubItems.Add("***" + device.LightModuleSN.Substring(3));

            this.listViewLockMac.Items.Add(item);
        }

        private void buttonChg_Click(object sender, EventArgs e)
        {
            string bosaSn = this.textBoxBosaSn.Text.Trim();
            string mac = this.textBoxMac.Text.Trim();
            if (string.IsNullOrEmpty(bosaSn) || bosaSn.Length != 12 || string.IsNullOrEmpty(mac))
            {
                MessageBox.Show("MAC、BOSA信息为空，或不为12位，解锁失败");
                return;
            }

            //判断BOSA和MAC是否已经绑定过了
            Device_Data deviceW = new Device_Data();
            deviceW.SetWhere();

            deviceW.MACAddr = mac;
            deviceW.LightModuleSN = bosaSn;

            var selectCountDevice = MethodWcf.SelectDeviceDataCountClient(deviceW);
            if (selectCountDevice.result && selectCountDevice.message == "1")
            {
                //修改产品表状态
                //修改Device_Data的SN为空
                deviceW.Clear();
                deviceW.SetWhere();
                deviceW.MACAddr = mac;

                deviceW.SetUpdate();
                deviceW.LightModuleSN = "";

                var updateDevice = MethodWcf.UpdateDeviceDataClient(deviceW);

                PRODUCT pro = new PRODUCT();
                pro.SetWhere();
                pro.MAC = mac;

                pro.SetUpdate();
                pro.BREAK_STATE = false;

                var updatePro = MethodWcf.UpdateProductClient(pro);

                if (updateDevice.result && updatePro.result)
                {
                    MessageBox.Show("解锁MAC成功");
                }
                else
                {
                    MessageBox.Show("解锁MAC失败");
                }

                this.textBoxBosaSn.Clear();
                this.textBoxMac.Clear();
                this.textBoxSearch.Clear();
            }
        }

        private void FormMac_Load(object sender, EventArgs e)
        {
            NODE nodeW = new NODE();
            var selectNode = MethodWcf.SelectNodeClient(nodeW);

            if (selectNode.result)
            {
                _listNode = selectNode.value;
            }

            this.buttonRefush.PerformClick();
        }

        private void listViewLockMac_MouseClick(object sender, MouseEventArgs e)
        {
            //找到选中的信息
            var listItem = this.listViewLockMac.SelectedItems;

            if (listItem != null && listItem.Count == 1)
            {
                this.textBoxMac.Text = listItem[0].Text;
            }
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            //搜索BOSA信息
            string search = this.textBoxSearch.Text.Trim();

            this.listViewLockMac.Items.Clear();
            if (string.IsNullOrEmpty(search))
            {
                for(int i = 0;i<_lockProduct.Count;i++)
                {
                    SetOneLockProduct(_lockProduct[i].MAC);
                }
                return;
            }

            var searchList = (from d in _listDevice
                              where string.Format("{0},{1}", d.MACAddr, d.LightModuleSN)
                              .Contains(search)
                              select d).ToList();

            if (searchList != null && searchList.Count > 0)
            {
                for(int i = 0; i < searchList.Count; i++)
                {
                    SetOneLockProduct(searchList[i].MACAddr);
                }
            }

        }
    }
}
